package com.GroupSix.controller;

import com.GroupSix.entity.DictEntity;
import com.GroupSix.service.DictService;
import com.GroupSix.vo.DictEntityVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("banked/dict")
public class DictController {
    @Autowired
    private DictService dictService;          //注入业务逻辑
//增
    @PostMapping("/add")
    public String add(@RequestBody DictEntity dictEntity){
        Integer i = dictService.add(dictEntity);
        if(i>0){
            return "success";
        }
        else {
            return "fail";
        }
    }

// 删：根据ID删除字典
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id) {
    int i = dictService.delete(id);
    if(i>0){
        return "success";
    }
    else {
        return "fail";
    }
}

// 查：根据ID获取字典
    @GetMapping("/{id}")
    public ResponseEntity<DictEntity> getDictById(@PathVariable Integer id) {
        DictEntity dict = dictService.search(id);
        return ResponseEntity.ok(dict);
    }

    // 改：更新字典
    @PutMapping("/update")
    public String update(@RequestBody DictEntity dict) {
        int i = dictService.update(dict);
        if(i>0){
            return "success";
        }
        else {
            return "fail";
        }
    }
    //分页查询
    @GetMapping("/List")
    public IPage<DictEntity> List(@RequestBody DictEntityVo vo){
        DictEntity dictEntity = new DictEntity();
        dictEntity.setDictName(vo.getDictName());
        return dictService.List(dictEntity, vo.getCurpage(),vo.getLimit());
    }
}
